import { _decorator, Component, Node, input, EventTouch, Input, SystemEvent, TiledUserNodeData } from 'cc';
import { gameManeger } from '../framework/gameManeger';
import { planeController } from '../planeController';
const { ccclass, property } = _decorator;

@ccclass('UImain')
export class UImain extends Component {
    @property
    public speed = 5;
    @property(Node)
    public plane: Node = null;
    @property(gameManeger)
    public gameManager: gameManeger = null;
    @property(planeController)
    public planeController: planeController = null;
    @property(Node)
    public Main: Node = null;
    @property(Node)
    public game: Node = null;
    @property(Node)
    public gameOver: Node = null;
    onEnable() {
        input.on(Input.EventType.TOUCH_START, this._touchStart, this);
        input.on(Input.EventType.TOUCH_MOVE, this._touchMove, this);
        input.on(Input.EventType.TOUCH_END, this._touchEnd, this);
    }
    onDisEnable() {
        input.off(Input.EventType.TOUCH_START, this._touchStart, this);
        input.off(Input.EventType.TOUCH_MOVE, this._touchMove, this);
        input.off(Input.EventType.TOUCH_END, this._touchEnd, this);
    }
    start() {
        this.Main.active = true;
    }

    update(deltaTime: number) {

    }
    private _touchMove(event: EventTouch) {
        if (!this.gameManager.isGameStart) {
            return;
        }
        const delta = event.getDelta();
        let pos = this.plane.position;
        this.plane.setPosition(pos.x + 0.01 * this.speed * delta.x, pos.y, pos.z - 0.01 * this.speed * delta.y);
    }
    private _touchStart(event: EventTouch) {
        if (this.gameManager.isGameStart) {
            this.planeController._isShooting = true;
        }
    }
    private _touchEnd(event: EventTouch) {
        if (this.gameManager.isGameStart) {
            this.planeController._isShooting = false;
        }
    }
    private soloGameStart() {//响应单人模式按钮事件
        this.Main.active = false;
        this.game.active = true;
        this.gameManager.gameStart();
    }
    private returnMain() {//响应返回主菜单按钮事件
        this.gameOver.active = false;
        this.Main.active = true;
        this.gameManager._init();
    }
}

